$(function(){
	var url={
		company_list:"{:url('adminapi/Company/companyList')}",
		state:"{:url('/adminapi/index/getConfig/name/check_state')}"
	}
	var companyload=layer.load()
	{if $Think.get.company_id}
	
		
	{else/}
	var addoperation = `<td class='operation'>
			<div class="tpl-table-black-operation">
				<a class="choose" href="javascript:;">
					<i class="am-icon-pencil"></i> 选中
				</a>
			</div>
		</td>`;
		var deptlist;
		var limitall;
		
		
		var page=1;
		var tddata=[
			{
				key:'company_name',
				type:1,
				url:''
			},{
				key:'company_mode',
				type:1,
				url:'{:url("detail")}',
				id:137
			},{
				key:'account',
				type:1,
				url:''
			},{
				key:'contacts',
				type:1,
				url:''
			},{
				key:'phone',
				type:1,
				url:''
			}];
	{/if}
	
	{if $Think.get.company_id}
	var data={
			page:1,
			pagenum:9999999
	};
	{else/}
	var data={
			page:1,
			pagenum:10
	};
	 	var where_json={};
	    {if $Think.get.company_name}
			where_json.company_name=['like','%{$Think.get.company_name}%']
		{/if}
		data.where_json=JSON.stringify(where_json)
	{/if}
	ajaxcompany(data)
	function ajaxcompany(data){
		
	$.ajax({
		type:"post",
		url:url.company_list,
		data:data,
		success:function(res){
			console.log(res)
			layer.close(companyload)
			companylist=``
			var  companylist=`<option value=''>全部</option>`;
			{if $Think.get.company_id}
			for(var key in res.data){
				if(res.data[key].company_id=={$Think.get.company_id}){
				  	companylist+=`<option selected value='${res.data[key].company_id}'>${res.data[key].company_name}</option>`;
				}else{
				 	companylist+=`<option value='${res.data[key].company_id}'>${res.data[key].company_name}</option>`;
				}
			}
			
			{else/}
			$('tbody').html(tbodymainf2(res.data, 1, tddata, addoperation, 'company_id'))
				$('.pagination').html(pagechang1(res.count, 10, data.page))
				$('.laquo').on('click', function() {
					page-=1
					data.page=page
					ajaxcompany(data)
				})
				$('.pageli').on('click', function() {
					page=$(this).html()
					data.page=page
					ajaxcompany(data)
				})
				$('.raquo').on('click', function() {
					page+=1
					data.page=page
					ajaxcompany(data)
				})
				$('.choose').on('click',function(){
					window.location.href=('{:url("admin/finance/accountchecking")}?year=2018&company_id='+$(this).closest('tr').data('id'))
				})
			{/if}
			$('select[name="company_id"]').html(companylist)
			
		}
	});
	}
	
	
	{if $Think.get.company_id}
	var selectload=layer.load()
	$.ajax({
		type:"post",
		url:url.state,
		success:function(res){
			console.log(res)
			layer.close(selectload)
			selectlist=``
			var  selectlist=`<option value=''>全部</option>`;
			for(var key in res.data){
				{if $Think.get.state}
				if((key)=={$Think.get.state}){
				  	selectlist+=`<option selected value='${key}'>${res.data[key]}</option>`;
				}else{
				 	selectlist+=`<option value='${key}'>${res.data[key]}</option>`;
				}
				{else/}
					selectlist+=`<option value='${key}'>${res.data[key]}</option>`;
				{/if}
			}
			$('select[name="state"]').html(selectlist)
			$('.am-datepicker-months tbody tr td span').on('click',function(){
					$('.Iclose').datepicker('close');
				})
			$('.car_use').on('click', function() {
				choosecar("{:url('used/index',['op'=>'search'])}?filter=order")
			});
			var arr;
			ajaxreconciliation(res.data)
		}
	});
	{/if}
	
	
	function ajaxreconciliation(state){
		
		var data1={}
		
		{if $Think.get.year}
		data1.year="{$Think.get.year}";
		{/if}
		{if $Think.get.company_id}
		data1.company_id="{$Think.get.company_id}";
		{/if}
		{if $Think.get.state}
		data1.state="{$Think.get.state}";
		{/if}
		var listload = layer.load();
		$.ajax({
			url: "{:url('adminapi/check/index')}",
			type:'post',
			data:data1,
			success: function(res) {
				console.log(res)
				layer.close(listload);
				var checklist=``;
				var statemain;
				var arr11=[];
				console.log(res.data)
				for(var ii2=0;ii2<12;ii2++){
					for(var key in res.data){
						if((key.slice(5)-0)==ii2+1){
							if(res.data[key].month){
							}else{
								res.data[key].month=key
							}
							arr11.push(res.data[key])
						}
						
						
						
					}
				}
				console.log(arr11)
				for(var accoidx=0;accoidx<arr11.length;accoidx++){
					if(arr11[accoidx].check_state){
						statemain=state[arr11[accoidx].check_state]
					}else{
						statemain='-'
					}
					checklist+=`
					<tr data-month='${arr11[accoidx].month}'>
						<td>${arr11[accoidx].month}</td>
						<td>{$Think.get.car_use_name}</td>
						<td class='ordertotal'>${arr11[accoidx].order_total?arr11[accoidx].order_total:'-'}</td>
						<td class='ordernumber'>${arr11[accoidx].order_number?arr11[accoidx].order_number:'-'}</td>
						<td class='state' data-state='${arr11[accoidx].state?arr11[accoidx].state:0}'>${statemain}</td>
						<td class='express'>${arr11[accoidx].express?arr11[accoidx].express:'-'}</td>`
					if(arr11[accoidx].check_state){
					
							var addoperation=`
						<td class='operation' data-id='${arr11[accoidx].id}'>
							<div class="tpl-table-black-operation">
								<a class="account">
									<i class="am-icon-pencil"></i> 对账单
								</a>
								<a class="express" data-state='${arr11[accoidx].check_state}'>
									<i class="am-icon-pencil"></i> 快递单
								</a>
								
							</div>
						</td>`
						
					}else{
						var addoperation=`
						<td class='operation'>
							<div class="tpl-table-black-operation">
								<a class="create">
									<i class="am-icon-pencil"></i> 生成对账单
								</a>
								
							</div>
						</td>`
					}
					checklist+=addoperation
					checklist+=`</tr>`
					
					
				}
				{if $Think.get.company_id}
				$('#tablea tbody').html(checklist)
				{/if}
				{if $Think.get.company_id}
				$('.create').on('click',function(){
					var that=this
					var month=$(this).closest('tr').data('month')
					console.log(month)
					var createload=layer.load()
					$.ajax({
						type:"post",
						url:"{:url('/adminapi/check/creat')}",
						data:{
							month:month 
							,company_id:{$Think.get.company_id}
							
						},
						success:function(res1){
							layer.close(createload)
							if(res1.code==1){
								console.log(res1)
								ajaxreconciliation(state)
//							
							}else{
								Err(res1.msg)
							}
						}
					});
				})
				{/if}
				$('.express').on('click',function(){
					console.log($(this).data('state'))
					var stateindex=$(this).data('state')
					var that=this
					if(stateindex==4){
						var expressprop=layer.open({
						  type: 1,
						  title:'快递单号',
						  skin: 'layui-layer-rim', //加上边框
						  area: ['300px', '200px'], //宽高
						  content: `<div class='am-u-sm-12' style='padding:10px'>
						 			 	<div class='am-u-sm-12'>
											<input class="express_input" style='width:100%'  type="number"/>
										</div>
										<div class='am-u-sm-12' style="text-align:center;margin-top:10px">
						   					<a class='am-btn am-btn-sm am-btn-primary am-radius express_ok'>提交</a>
						   					<a class='am-btn am-btn-sm am-btn-primary am-radius express_no'>取消</a>
						   			</div>
									</div>		
						  `,
						  success:function(){
						  	$('.express_ok').on('click',function(){
						  		console.log($('.express_input').val())
						  		var tmid=$(that).closest('.operation').data('id')
						  		console.log(tmid)
						  		var tmload=layer.load()
						  		$.ajax({
						  			type:"post",
						  			data:{
						  				id:tmid,
						  				express:$('.express_input').val()
						  			},
						  			url:'{:url("adminapi/Check/express")}',
						  			success:function(res){
						  				console.log(res)
						  				layer.close(tmload)
						  				if(res.code==1){
						  					Msg('快递单已提交')
						  					layer.close(expressprop)
						  					ajaxreconciliation(state)
						  				}else{
						  					Err(res.msg)
						  				}
						  			},
						  			error:function(){
						  				layer.close(tmload)
						  				Err('请求失败')
						  			}
						  		})
						  	})
						  	$('.express_no').on('click',function(){
						  		console.log($('.express_input').val())
						  		layer.close(expressprop)
						  	})
						  	
						  }
						});
		//				layer.open({
		//				content: '快递单号:',
		//				btn: ['确定']
		//				})
					}else{
						var ex11=layer.open({
						content: '请确认订单',
						btn: ['确定'],
						yes: function() {
							
								layer.close(ex11)
		//						$.ajax({
		//							type:"post",
		//							url:"{:url('adminapi/Check/cancel')}",
		//							data:{
		//								id:invalididx
		//							},
		//							success:function(res2){
		//								console.log(res2)
		//								if(res2.code==1){
		//									accdetail()
		//								}
		//							}
		//						});
							}
						})
					}
				})
				$('.account').on('click',function(){
					console.log($(this).parent().parent().data('id'));
					window.location.href="/admin/Finance/AccountCheckingdetails?id="+$(this).parent().parent().data('id'); 
				})
				
			}
		})
		
	}
	function accmain(){
		$('.account').unbind()
		$('.account').on('click',function(){
			console.log($(this).parent().data('id'));
//			window.location.href="/admin/Finance/AccountCheckingdetails?id="+$(this).parent().data('id'); 
		})
		$('.express').unbind()
		$('.express').on('click',function(){
			var stateindex=$(this).data('state')
			if(stateindex==4){
				var expressprop=layer.open({
				  type: 1,
				  title:'快递单号',
				  skin: 'layui-layer-rim', //加上边框
				  area: ['200px', '200px'], //宽高
				  content: `<div class='am-u-sm-12'>
				 			 	<div class='am-u-sm-12'>
									<input class="express_input" type="number"/>
								</div>
								<div class='am-u-sm-12' style="text-align:center;margin-top:10px">
				   					<a class='am-btn am-btn-sm am-btn-primary am-radius express_ok'>提交申请</a>
				   					<a class='am-btn am-btn-sm am-btn-primary am-radius express_no'>取消</a>
				   			</div>
							</div>		
				  `,
				  success:function(){
				  	$('.express_ok').on('click',function(){
				  		console.log($('.express_input').val())
				  		layer.close(expressprop)
				  	})
				  	$('.express_no').on('click',function(){
				  		console.log($('.express_input').val())
				  		layer.close(expressprop)
				  	})
				  	
				  }
				});
//				layer.open({
//				content: '快递单号:',
//				btn: ['确定']
//				})
			}else{
				layer.open({
				content: '请确认订单',
				btn: ['确定'],
				yes: function() {
					
						var invalidload=layer.load()
//						$.ajax({
//							type:"post",
//							url:"{:url('adminapi/Check/cancel')}",
//							data:{
//								id:invalididx
//							},
//							success:function(res2){
//								console.log(res2)
//								if(res2.code==1){
//									accdetail()
//								}
//							}
//						});
					}
				})
			}
		})
	}
})
